package io.github.bomb.batch.application;


import io.github.bomb.batch.domain.BatchTask;
import io.github.bomb.batch.infrastructure.factory.JobFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.stereotype.Service;

/**
 * TaskExecutionService
 * 负责执行批处理任务。
 */
@Service
public class TaskExecutionService {

	private final JobLauncher jobLauncher;
	private final JobFactory jobFactory;

	public TaskExecutionService(JobLauncher jobLauncher, JobFactory jobFactory) {
		this.jobLauncher = jobLauncher;
		this.jobFactory = jobFactory;
	}

	/**
	 * 执行指定的批处理任务。
	 *
	 * @param batchTask 批处理任务
	 */
	public void executeTask(BatchTask batchTask) throws Exception {
		Job job = jobFactory.createJob(batchTask);
		jobLauncher.run(job, new JobParametersBuilder()
				.addLong("time", System.currentTimeMillis())
				.toJobParameters());
	}
}
